<?php
namespace app\common\service;

use app\common\util\Upload;
use app\common\util\Util;
use think\Model;
use think\Request;

class ThemeService extends BaseService
{


    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 主题列表
     */
    public function getThemeList(Request $request)
    {
        $sid = $request->param('sid');
        $where = [
            'sid' => $sid,
            'status' => 1
        ];

        return $this->GetBaseThemeList($where,$sid);
    }

    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 主题详情
     */
    public function themeDetail(Request $request)
    {
        $sid = $request->param('sid');
        $id = $request->param('id');

        $where = [
            'sid' => $sid,
            'id' => $id,
            'status' => 1
        ];
        $detail = db()->name('user_students_theme')->where($where)->find();
        $data = ['status'=>1,'data'=>$detail];
        return $data;
    }

    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 选择主题
     */
    public function SelectTheme(Request $request)
    {
        $sid = $request->param('sid');
        $uid = $request->param('uid');
        $id = $request->param('id');

        //获得主题init_id
        $init_id = db('user_students_theme')->where(['id' => $id])->value('init_id');
        $where = [
            'sid' => $sid,
            'uid' => $uid

        ];

        $res = db()->name('students')->where($where)->update(['homepage_style'=>$init_id]);
        if($res===false){
            $data = ['status'=>0];
        }else{
            $data = ['status'=>1];
        }

        return $data;
    }

    /**
     * 后台获取主题列表
     * @param $sid
     * @return array
     */
    public function AdminGetThemeList($sid){
        //students_theme表中的数据
        $where = [
            'sid' => $sid
        ];
        $theme_list = db('user_students_theme')
                ->where($where)
                ->field('init_id')
                ->select();
        //init表中的数据
        $init_list = db('user_students_theme_init')->where(['status' => 1])->select();
        if(count($theme_list) < count($init_list)){
            if(empty($theme_list)){
                $ids = [0];
            }else{
                foreach($theme_list as $v){
                    $ids[] = $v['init_id'];
                }
            }

            foreach($init_list as $val){
                if(!in_array($val['init_id'],$ids)){
                    //没有该主题，则添加
                    $add_data[] = [
                        'init_id' => $val['init_id'],
                        'sid' => $sid,
                        'theme_name' => $val['theme_name'],
                        'theme_img' => $val['theme_img'],
                        'theme_author' => $val['theme_author'],
                        'status' => $val['status'],
                        'addtime' => time()
                    ];
                }
            }

            if(!empty($add_data)){
                db('user_students_theme')->insertAll($add_data);
            }
        }
        $where = [
            'sid' => $sid,
            'status' => ['in',[0,1]]
        ];
        return $this->GetBaseThemeList($where,$sid);

    }

    public function GetBaseThemeList($where,$sid){
        $list_count = db()->name('user_students_theme')->where($where)->count();
        $list = db()->name('user_students_theme')->order('init_id Desc')->where($where)->select();
        //使用人数
        if(!empty($list)){
            foreach($list as $k => $v){
                $list[$k]['use_num'] = db('students')->where(['sid' => $sid,'homepage_style' => $v['id']])->count();
            }
        }
        $data = ['status'=>1,'data'=>['content' => $list,'page' =>['count' =>$list_count]]];
        return $data;
    }

    /**
     * 修改主题状态
     * @param $sid
     * @param $id
     * @param $status
     * @return array
     * @throws \think\Exception
     */
    public function EditThemeStatus($sid,$id,$status){
        $where = [
            'sid' => $sid,
            'id' => $id
        ];
        $res = db('user_students_theme')->where($where)->update(['status' => $status]);
        if($res === false){
            return ['status' => 0];
        }else{
            return ['status' => 1];
        }
    }

    /**
     * 设置默认主题
     * @param $sid
     * @param $id
     * @return array
     * @throws \think\Exception
     */
    public function SelectThemeDefault($sid,$id){
        $where = [
            'sid' => $sid
        ];
        db('user_students_theme')->where($where)->update(['default_status'=>0]);
        $where['id'] = $id;
        $res = db('user_students_theme')->where($where)->update(['default_status'=>1]);
        if($res === false){
            return ['status' => 0];
        }else{
            $info['content'] = db('user_students_theme')->where($where)->find();
            return ['status' => 1,'data' => $info];
        }
    }


}






















